package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.service.family.LoanAdvertService;
import com.tuanzi.loan.business.service.family.ProductInfoService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.ProductInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.ProductInfoSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.family.ProductInfoLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.ONLINE_OFFLINE_STATUS;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class FirstPartyTagListBean extends JSFListingSessionBean<ProductInfoSearchRequest, ProductInfoSearchResponse> {

	private static final long serialVersionUID = -2152778554108785342L;
	@Autowired
	private ProductInfoService productInfoService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private LoanAdvertService loanAdvertService;

	public void onReset() {
		initLazyDataModel();
	}

	public String onAdd() {
		return redirect("/views/family/tags/first-party/info.xhtml");
	}

	public String onEdit(String id) {
		return redirect("/views/family/tags/first-party/info.xhtml?id=" + id);
	}

	public void onOnline(Long id) {
		productInfoService.online(productInfoService.findOne(id));
		addSuccessfullyMessage();
	}
	
	public String onCopy(Long id) {
		Long newPorductInfoId = productInfoService.copyProductInfo(id);
		return redirect("/views/family/tags/first-party/info.xhtml?id=" + newPorductInfoId);
	}

	public void onOffline(Long id) {
		productInfoService.offline(productInfoService.findOne(id));
		// 贷款列表对应的活动同时下线
		loanAdvertService.offline(id);
		addSuccessfullyMessage();
	}

	public void onBatchOnline() {
		List<ProductInfo> onlineProductInfos = getSelectedProductInfos();
		productInfoService.online(onlineProductInfos);
		addSuccessfullyMessage();
	}

	public void onBatchOffline() {
		List<ProductInfo> offlineProducdtInfos = getSelectedProductInfos();
		productInfoService.offline(offlineProducdtInfos);
		addSuccessfullyMessage();
	}

	private List<ProductInfo> getSelectedProductInfos() {
		return productInfoService
				.findAll(selectedRecords.parallelStream().map(ProductInfoSearchResponse::getId).map(x -> NumberUtils.toLong(x.toString())).collect(Collectors.toList()));
	}

	@Override
	protected String page() {
		return "/views/family/tags/first-party/list.xhtml";
	}

	@Override
	protected void initLazyDataModel() {
		$ = new ProductInfoSearchRequest();
		lazyDataModel = new ProductInfoLazyDataModel($, session, productInfoService);

	}
	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(ONLINE_OFFLINE_STATUS);
	}
}
